/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Initial Developer of the Original Code is
*   Nick Kreeger
* Portions created by the Initial Developer are Copyright (C) 2006-2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*   Nick Kreeger <nick.kreeger@park.edu>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */

#include "CHMsgDBViewService.h"

#import "CHMsgService.h"

#import "nsServiceManagerUtils.h"
#import "nsString.h"

#import "nsMsgBaseCID.h"

#import "nsISupportsArray.h"

const char* kMsgDBViewBaseContractID = "@mozilla.org/messenger/msgdbview;1?type=";

class nsIMessenger;
class nsIMsgWindow;

NS_IMPL_ISUPPORTS1(CHMsgDBViewService, nsISupports)

/**
 * Constructor.
 */
CHMsgDBViewService::CHMsgDBViewService(CHMsgService* aMsgService)
: mMsgService(aMsgService)
{
}

/**
 * Destructor.
 */
CHMsgDBViewService::~CHMsgDBViewService()
{
}

void
CHMsgDBViewService::CreateDBView(nsIMsgFolder* aMsgFolder, 
                                 nsMsgViewTypeValue* aViewType,
                                 nsMsgViewFlagsTypeValue* aViewFlags, 
                                 nsMsgViewSortTypeValue* aSortType, 
                                 nsMsgViewSortOrderValue* aSortOrder)
{
  // Call the inner create method
  CreateBareDBView(nsnull, aMsgFolder, aViewType, aViewFlags, aSortType, aSortOrder);
  
  //XXX Update and sort indicators.
}

void
CHMsgDBViewService::CreateBareDBView(nsIMsgDBView* aOriginalView,
                                     nsIMsgFolder* aMsgFolder,
                                     nsMsgViewTypeValue* aViewType,
                                     nsMsgViewFlagsTypeValue* aViewFlags, 
                                     nsMsgViewSortTypeValue* aSortType, 
                                     nsMsgViewSortOrderValue* aSortOrder)
{
  nsCAutoString dbContractID(kMsgDBViewBaseContractID);
  nsMsgViewType msgViewType;
  nsMsgViewFlagsType msgsViewFlagsType;
  nsresult rv;
  
  /*
  XXX |msgViewType| cannont be resolved as a type in GCC 4.0 (intel)
  // This has to be hacked to a PRUint32 to avoid compiler issues.
  switch (*aViewType)
  {
    case msgViewType.eShowQuickSearchResults:
      dbContractID.Append("quicksearch");
      break;
    case msgViewType.eShowSearch:
      dbContractID.Append("search");
      break;
    case msgViewType.eShowThreadsWithUnread:
      dbContractID.Append("threadswithunread");
      break;
    case msgViewType.eShowWatchedThreadsWithUnread:
      dbContractID.Append("watchedthreadswithunread");
      break;
    case msgViewType.eShowVirtualFolderResults:
      dbContractID.Append("xfvf");
      break;
    case msgViewType.eShowAllThreads:
    default:
      if (*aViewFlags & msgsViewFlagsType.kGroupBySort)
        dbContractID.Append("group");
      else
        dbContractID.Append("threaded");
  }

  if (!aOriginalView)
    mMsgDBView = do_CreateInstance(dbContractID.get(), &rv);
  
  mCurViewFlags = *aViewFlags;
  mCurSortType = *aSortType;
  PRInt32 count;
  
  // var count = new Object();
  //if (!mThreadPaneCmdUpdater)
  //  mThreadPaneCmdUpdater = new CHMsgDBViewCommandUpdater();
  
  nsCOMPtr<nsIMessenger> messenger;
  rv = mMsgService->GetMessenger(getter_AddRefs(messenger));
  if (NS_FAILED(rv))
    return;
  
  nsCOMPtr<nsIMsgWindow> msgWindow = do_GetService(NS_MSGWINDOW_CONTRACTID, &rv);
  if (NS_FAILED(rv))
    return;
  /*
  if (!aOriginalView)
  {
    rv = mMsgDBView->Init(messenger, msgWindow, mThreadPaneCmdUpdater);
    if (NS_FAILED(rv))
      return;
    
    rv = mMsgDBView->Open(aMsgFolder, mCurSortType, *aSortOrder, mCurViewFlags, &count);
    if (NS_FAILED(rv))
      return;
    
    if (*aViewType == msgViewType.eShowVirtualFolderResults)
    {
      /*
       // the view is a listener on the search results
       gViewSearchListener = gDBView.QueryInterface(Components.interfaces.nsIMsgSearchNotify);
       gSearchSession.registerListener(gViewSearchListener);
       *
    }
  }
  else
  {
    aOriginalView->CloneDBView(messenger,
                               msgWindow,
                               mThreadPaneCmdUpdater,
                               getter_AddRefs(mMsgDBView));
  }
       */
}

void
CHMsgDBViewService::MarkCurrentMessageAsRead()
{
  // Sweet this worked!
  //nsMsgViewCommandType msgViewCommandType;
  //mMsgDBView->DoCommand(msgViewCommandType.markMessagesRead);
}

nsCString* 
CHMsgDBViewService::GetCurrentMessageUri()
{
  return mCurrentMessageUri;
}
